package com.mi.rof.user.controller;

import com.mi.rof.entity.param.PageParam;
import com.mi.rof.entity.vo.UserInfoVo;
import com.mi.rof.user.service.OrderService;
import com.mi.rof.entity.vo.OrderVO;
import com.mi.rof.entity.Result;
import com.mi.rof.entity.param.OrderParam;
import com.mi.rof.utils.RocketMQTopic;
import com.mi.rof.utils.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : Yang Yu you
 */
@RestController
@RequestMapping("/order")
@Api("订单管理")
public class OrderController {
    @Resource
    private OrderService orderService;

    @Resource
    private RocketMQTemplate rocketMqTemplate;

    @PostMapping("/insertOrder")
    @ApiOperation("新增工单")
    public Result<OrderVO> insertOrder(@RequestBody OrderParam orderParam) {
        Message<OrderParam> message = MessageBuilder.withPayload(orderParam).build();
        rocketMqTemplate.send(RocketMQTopic.SEND_ORDER_TOPIC, message);
        return Result.successResult();
    }

    @PostMapping("/updateOrder")
    @ApiOperation("修改工单")
    public Result<String> updateOrder(@RequestBody OrderParam orderParam) {
        return orderService.updateOrder(orderParam);
    }

    @PostMapping("/queryOrder")
    @ApiOperation("根据id查询工单")
    public Result<OrderVO> queryOrderById(@RequestParam String repairOrderId) {
        OrderVO orderVO = orderService.queryOrderById(repairOrderId);
        return Result.successResult(orderVO);
    }

    @PostMapping("/listOrder")
    @ApiOperation("根据id查询工单")
    public Result<List<OrderVO>> listOrder() {
        UserInfoVo userInfoVo = UserThreadLocal.get();
        PageParam pageParam = new PageParam();
        List<OrderVO> orderVos = orderService.listOrder(pageParam, userInfoVo.getUserId());
        return Result.successResult(orderVos);
    }

    @PostMapping("/deleteOrder")
    @ApiOperation("删除工单")
    public Result<Integer> deleteOrder(@RequestParam String repairOrderId){
        Integer result = orderService.deleteOrder(repairOrderId);
        return Result.successResult(result);
    }

}
